"use strict";
var utils = require('../../utils/utils.js');
var request = require('../../utils/request.js');
var auth = require('../../utils/auth.js');
var config = require('../../config.js');
var modal = require('../../utils/modal.js')
var app = getApp();
const ZERO = 0;
console.log('toJump', getApp())
var pageSetting = {
  // 页面显示后，进行登录和链接，完成后开始启动游戏服务

  data: {
    curData: {
      avatar: '',
      user_name: '',
      company: '',
      id: ''
    },
    account: [],
    curIndex: 0,
    money: '',
    name: '',
    hidePage: true,
    isShow: false,
    current_data: {},
    yesterday_data: {},
    iconNav: [{
      icon: '../../images/ic-home-shou@2x.png',
      name: '收银',
      url: '1',
      disable: true
    }, {
      icon: '../../images/ic-home-gongdan@2x.png',
      name: '工单中心',
      url: '2',
      disable: true
    }, {
      icon: '../../images/ic-home-huiyuan@2x.png',
      name: '会员管理',
      url: '3',
      disable: true
    }, {
      icon: '../../images/ic-home-yuyue@2x.png',
      name: '预约',
      url: '',
      disable: false
    }, {
      icon: '../../images/ic-home-hexiao@2x.png',
      name: '核销',
      url: '',
      disable: false
    }]
  },
  //选择区域
  //  selectOk: function(event){
  //    var selectId = event.target.dataset.id;
  //    for (var i = 0; i < this.data.items.length; i++) {
  //      if (this.data.items[i].id == selectId) {
  //        this.data.items[i].checked = true;
  //        this.data.name = this.data.items[i].name;
  //        console.log(this.data.name);
  //      } else {
  //        this.data.items[i].checked = false;
  //      }
  //    }
  //    this.setData({
  //      items:this.data.items,
  //    })
  // },
  onLoad() {
    
    // that.init();

  },
  loadData(){
    //有服务的时候
    var that = this;
    var data = {
      "shop_oid": "GSID8QEW04OO0"
    }
    modal.loading()
    request.post({ path: config.home }, data).then(res => {
      modal.hide()
      that.setData({
        current_data: res.data.data.current_data,
        yesterday_data: res.data.data.yesterday_data,
      })

    })

  },
  showModal() {
    this.setData({
      isShow: true
    })
  },
  close() {
    this.setData({
      isShow: false
    })
  },
  linkto(e) {
    var url = e.currentTarget.dataset.url
    switch (url) {
      case '1':
        wx.navigateTo({
          url: '../to-money/to-money',
        })
        break;
      case '2':
        wx.navigateTo({
          url: '../work-center/work-center',
        })
        break
      case '3':
        wx.navigateTo({
          url: '../member-manage/member-manage',
        })
        break;
      case '4':

      break;
      case '5':
        this.close()
        wx.navigateTo({
          url: '../create-order/create-order',
        })
      break
      case '6':
        this.close()
        wx.navigateTo({
          url: '../insurance/insurance',
        })
      break;
      default:
      break;
    }

  },
  goShouye() {
    wx.redirectTo({
      url: '/pages/to-money/to-money'
    })
  },
  returnFalse(){
    return false
  },
  onShow() {
    var that = this;

    that.setData({
      'money': '',
      'name': ''
    })
    var next = function () {
      // console.log('调用next方法')
      var avatar = wx.getStorageSync('user_info')['head_img'];
      that.setData({
        'curData.avatar': avatar,
        hidePage: false
      })
      that.getAccountInfo();


      that.loadData()
    }
    auth(next);


    var animation = wx.createAnimation({
      duration: 500,
      transformOrigin: "50% 50%",
      timingFunction: 'ease',
    })
    that.animation = animation;
  },
  onHide() {

  },
  init() {
    new app.WeToast();
  },
  bindInputTap(e) {
    var value = e.detail.value;
    var cinput = e.currentTarget.dataset.cinput;
    if (cinput == 'money') {

      this.setData({
        'money': this.clearNoNum(value)
      })
    } else if (cinput == 'name') {
      this.setData({
        'name': value
      })
    }
  },
  clearNoNum(value) {
    value = value.replace(/[^\d.]/g, "");  //清除“数字”和“.”以外的字符
    value = value.replace(/^\./g, "0."); //如果第一位就是. 变成0.
    value = value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
    value = value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
    value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');//只能输入两个小数
    if (value.indexOf(".") < 0 && value != "") {//以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
      value = parseFloat(value.substring(0, 5));//最长整数位5位数
    }
    return value;
  },
  formSubmit(e) {
    var that = this;
    var data = e.detail.value;

    if (!that.data.money) {
      wx.showToast({
        icon: 'none',
        title: '付款金额必填'
      })
      return false;
    }

    if (parseFloat(that.data.money).toFixed(2) <= ZERO.toFixed(2)) {
      wx.showToast({
        icon: 'none',
        title: '付款金额错误'
      })
      return false;
    }

    wx.navigateTo({
      url: `/pages/collection/collection?money=${data.money}&name=${data.name}&id=${data.id}`
    });

  },
  scanTap(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;

    if (!that.data.money) {
      wx.showToast({
        icon: 'none',
        title: '付款金额必填'
      })
      return false;
    }

    if (parseFloat(that.data.money).toFixed(2) <= ZERO.toFixed(2)) {
      wx.showToast({
        icon: 'none',
        title: '付款金额错误'
      })
      return false;
    }
    wx.scanCode({
      success: function (res) {

        var data = {
          id: id,
          code: res.result,
          money: that.data.money,
          name: that.data.name
        }

        //支付
        request.get({ 'path': config.createOrder }, data).then(function (res) {
          var ratingData = res.data;
          if (ratingData.ret == 0) {
            wx.navigateTo({
              url: `/pages/list/list/list?id=${id}money=${that.data.money}`
            })
          } else {
            wx.showToast({
              icon: 'none',
              title: ratingData.msg
            })
            return false;
          }
        }, function (res) {
          wx.showToast({
            icon: 'none',
            title: res.data.msg
          })
          return false;
        })
      },
      fail: function (res) {
        wx.showToast({
          icon: 'none',
          title: '支付失败'
        })
      }
    })
  },
  goRecordTap(e) {
    var id = e.currentTarget.dataset.id;
    if (id) {
      wx.navigateTo({
        url: `/pages/list/list/list?id=${id}`
      })
    }
  },
  goOcr(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/ocr/ocr?id=${id}`
    })
  },
  startProgressAnimation(isProgressShow) {
    var that = this
    if (isProgressShow) {
      that.animation.bottom(0).step()
    } else {
      that.animation.bottom(-100 + '%').step()
    }
    that.setData({
      animationProgress: that.animation.export()
    })
  },
  showProgressTap(e) {
    this.startProgressAnimation(true);
  },
  hideProgressTap: function (e) {
    this.startProgressAnimation(false);
  },
  switchAccountTap(e) {
    var index = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.id;
    var username = e.currentTarget.dataset.username;
    var company = e.currentTarget.dataset.company;

    this.setData({
      'curIndex': index,
      'curData.user_name': username,
      'curData.id': id,
      'curData.company': company
    });

    this.hideProgressTap();
  },
  changeAccountTap() {
    this.showProgressTap(true);
  },
  getAccountInfo() {
    var that = this;
    var mobile = wx.getStorageSync('mobile');
    request.get({ 'path': config.getList }, { mobile: mobile }).then(function (res) {
      var ratingData = res.data;
      if (ratingData.ret == 0 && ratingData.data.length > 0) {
        var curData = ratingData.data[0];

        that.setData({
          'account': ratingData.data,
          'curData.user_name': curData['user_name'],
          'curData.company': curData['company'],
          'curData.id': curData['id']
        });

        that.hideProgressTap();
      } else {
        wx.showToast({
          icon: 'none',
          title: ratingData.msg
        })
        return false;
      }
    }, function (res) {
      wx.showToast({
        icon: 'none',
        title: res.data.msg
      })
      return false;
    })
  }
}
utils.assign(pageSetting);

Page(pageSetting);
